home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / util / misc / VMM_V3_1.lha / VMM / doc / VMProgGuideline < prev    next >
Text File  |  1995-05-23  |  4KB  |  108 lines

  1.                              $Date: 95/05/23 10:22:30 $
  2.  
  3. If  you  are  writing programs, which use a lot of memory, such as ray
  4. tracers,  compilers,...   the  following guidelines will enable you to
  5. take advantage of virtual memory in a safe way:
  6.  
  7. *********************************************************************
  8.  
  9. Don't  access  virtual memory inside forbidden/disabled sections.  The
  10. following example shows you, why this is necessary:
  11.  
  12. /* This example should read the names of all currently waiting tasks */
  13.  
  14. UBYTE *buffer;
  15. UBYTE *buffer_ptr;
  16. struct Task *tmpTask;
  17.  
  18. /* The following request is allocated as VM */
  19. if ((buffer = AllocMem (SOME_SIZE, MEMF_ANY)) == NULL)      
  20.   return (FALSE);
  21.  
  22. buffer_ptr = buffer;
  23.  
  24. Forbid ();
  25. for (tmpTask = (struct Task*)SysBase->TaskWait.lh_Head;
  26.      tmpTask->tc_Node.ln_Succ != NULL;
  27.      tmpTask = tmpTask->tc_Node.ln_Succ)
  28.   {
  29.   strcpy (buffer_ptr, tmpTask->tc_Node.ln_Name);  /* can cause page-fault */
  30.   buffer_ptr += strlen (tmpTask->tc_Node.ln_Name) + 1);
  31.   }
  32. Permit ();
  33.  
  34. The  preceding example might produce a page-fault while copying a task
  35. name  to  the internal buffer.  A page-fault results in task switching
  36. and  subsequently  the ln_Next pointer is invalid, when the page-fault
  37. has completed. A crash may result.
  38.  
  39. *********************************************************************
  40.  
  41. Don't  allocate  messages,  IORequests,  task  structures etc.  on the
  42. stack,  if  you  are  not absolutely sure that your stack is in public
  43. memory.
  44. E.g.,  if  you  allocate an IORequest on your stack, which might be in
  45. virtual  memory,  and you send this IORequest to the timer.device, the
  46. machine  might  crash,  because the timer.device accesses its requests
  47. from  an  interrupt routine.  Page-faults while in supervisor mode are
  48. deadly.
  49.  
  50. *********************************************************************
  51.  
  52. Don't  access  virtual  memory  from  supervisor  mode.  This includes
  53. interrupt  and trap-handling routines.  Because the task producing the
  54. page-fault  is  put  into  the  wait  state,  a page-fault may only be
  55. invoked   by   a   task   using  its  standard  user  stack,  not  the
  56. (system-global) supervisor stack.
  57.  
  58. *********************************************************************
  59.  
  60. Don't   allocate   or   free   non-public   memory   from   inside   a
  61. forbidden/disabled   section.    Requests   inside  forbidden/disabled
  62. sections are simply not granted virtual memory, freeing virtual memory
  63. is  postponed  until  that  section  is  over.   It does not crash the
  64. machine, it simply isn't recommended.
  65.  
  66. *********************************************************************
  67.  
  68. Memory  allocated  with  the  MEMF_PUBLIC  flag  cleared should not be
  69. accessed  by  tasks  not  belonging to your program.  I.e.  don't read
  70. from a file using a buffer of virtual memory.  You never know in which
  71. way  another task or program accesses your buffer.  It might access it
  72. from within an interrupt.
  73.  
  74. *********************************************************************
  75.  
  76. Localize  your  data.  This means, if you are writing a program, which
  77. consists  of  several passes, try to put the data needed for each pass
  78. together in address space.  This greatly reduces the page-fault rate.
  79.  
  80. *********************************************************************
  81.  
  82. If you need to change your stack pointer, use a MEMF_PUBLIC area. The
  83. code below could be used (StackSwap() is in ROMS >= 2.0). 
  84.  
  85.         if (newstack = AllocMem(__stack, MEMF_PUBLIC))
  86.         {
  87.             stackStruct.stk_Lower = newstack;
  88.             stackStruct.stk_Upper = newstack + __stack;
  89.             stackStruct.stk_Pointer = stackStruct.stk_Upper;
  90.             StackSwap(&stackStruct);
  91.         }
  92.  
  93. ...
  94.  
  95. (In cleanup routine:)
  96. ...
  97.         if (newstack)
  98.         {
  99.             StackSwap(&stackStruct);
  100.             FreeMem(newstack, __stack);
  101.         }
  102.  
  103. (code by Jim Cooper)
  104.  
  105. *********************************************************************
  106.  
  107. Apart from this, always follow Commodore's programming guideline.
  108.